import random

WIDTH = 400
HEIGHT = 400

BACK_COLOR = (0, 0, 0)

class MyRect:
    def __init__(self, x, y, w, h, clr):
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        self.clr = clr
        self.speed = 2
    def SetSpeed(self, speed):
        self.speed = speed
        
    def Update(self):
        pass

    def GetGridPos(self):
        gx = self.x // self.w
        gy = self.y // self.h

        return gx, gy

    def Draw(self):
        rc = Rect((self.x - self.w // 2, self.y - self.h // 2), (self.w, self.h))
        screen.draw.filled_rect(rc, self.clr)

rc_list = []
unit = 50
def reset():
    clr_list = [
        (0, 200, 0),
        (200, 0, 0),
        (0, 0, 200)
    ]
    clist = []
    for r in range(0, 8):
        for c in range(0, 8):
#            clr = clr_list[c]
            clr = random.randint(0,2)
            rc = MyRect(r * unit + unit // 2, c * unit + unit // 2, unit, unit, clr_list[clr])
            rc_list.append(rc)
            clist.append(clr_list[clr])
    return clist
'''        
        head = clr_list[0]
        clr_list = clr_list[1:]
        clr_list.append(head)
'''
clist = reset()
def on_key_down(key):
    if key == keys.ESCAPE:
        quit()
    elif key == keys.SPACE:
        reset()
        
def on_mouse_down(pos):
    print("mouse_down", pos)
    x = pos[0]
    y = pos[1]
    gx = x // unit
    gy = y // unit
    print(gx, gy)

    for rc in rc_list:
        rcgx, rcgy = rc.GetGridPos()
        #print(rcgx, rcgy)
        if rcgx == gx and rcgy == gy:
            rc_list.remove(rc)
            print(len(rc_list))
            pvc(gx,gy)
            return gx,gy
            break

def pvc(gx,gy):
    print("PVC!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
    AI = False
    smartval = random.randint(0,100)
    if smartval >= 0 and smartval < 80:
        smart = False
    else:
        smart = True
    i = gx * 8 + gy
    val = clist[i]
    player_score = "player score:"
    AI_score = "AI score:"
    if smart == True:
        AI_score += "5"
    else:
        found = False
        while found == True:
            gx = random.randint(0,8)
            gy = randon.randint(0,8)
            for i in rc_list:
                rcgx,rcgy = i.GetGridPos()
                if rcgx == gx and rcgy == gy:
                    found = True
                    break
    rand = random.randint(1,3)
    i = gx * 8 + gy
    val = clist[i]
    if val == (200,0,0):
        if AI == False:
            player_score += "5"
        else:
            AI_score += "5"
    elif val == (0,200,0):
        if AI == False:
            player_score += "3"
        else:
            AI_score += "3"
    elif val == (0,0,200):
        if AI == False:
            player_score += "1"
        else:
            AI_score += "1"
    if AI == True:
        AI = False
    else:
        AI = True
    screen.draw.text(player_score,topleft = (0,0))
    screen.draw.text(AI_score,topright = (0,399))
def on_mouse_up(pos):
    print("mouse_up", pos)
    
def update():
    pass
        
def draw():
    screen.clear()
    screen.fill(BACK_COLOR)
    for rc in rc_list:
        rc.Draw()
